import PIL.Image
import dlib
import numpy as np

"""
功能：人脸检测 获取人脸的位置
"""

# 图片名
file_name = "image/001.jpg"

# HOG 分类器
face_detector = dlib.get_frontal_face_detector()

win = dlib.image_window()

# image = face_recognition.load_image_file(file_name)
im = PIL.Image.open(file_name)
image = np.array(im)

detected_faces = face_detector(image, 1)

print("图片{}中有{}个人脸".format(file_name, len(detected_faces)))

# 显示图片
win.set_image(image)

for i, face_rect in enumerate(detected_faces):
    # Detected faces are returned as an object with the coordinates
    # 输出人脸位置信息
    print("- 人脸 #{} 位置坐标 Left: {} Top: {} Right: {} Bottom: {}".format(i, face_rect.left(), face_rect.top(),
                                                                       face_rect.right(), face_rect.bottom()))

    # 将脸框起来
    win.add_overlay(face_rect)

# 回车退出
dlib.hit_enter_to_continue()
